package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.employee;
import com.reggie.service.employeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping(value = "/employee")
@Transactional
public class employeeController {
    @Autowired
    private employeeService employeeService;

    /**
     * @param emp
     * @param request
     * @return com.reggie.common.R<com.reggie.entity.employee>
     * @description: 后台员工登录使用
     * @author 86187
     * @date: 2022/11/15 13:15
     */
    @PostMapping(value = "/login")
    public R<employee> toLogin(@RequestBody employee emp, HttpServletRequest request) {
        R<employee> res;
        System.out.println(emp);
        String password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        LambdaQueryWrapper<employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(employee::getUsername, emp.getUsername());
        employee result = employeeService.getOne(lqw);
        if (result == null) {
            res = R.error("不存在用戶");
        } else if (!result.getPassword().equals(password)) {
            res = R.error("密碼出現錯誤");
        } else if (!result.getStatus().equals(1)) {
            res = R.error("賬戶已被鎖定");
        } else {
            request.getSession().setAttribute("employee", result.getId());
            //设置session的存活时间
            request.getSession().setMaxInactiveInterval(60 * 60 * 24);
            res = R.success(emp);
        }
        log.info(result.getId() + "登录中...");
        return res;
    }

    /**
     * @param request
     * @return com.reggie.common.R<java.lang.String>
     * @description: 员工选择退出登录
     * @author 86187
     * @date: 2022/11/15 19:34
     */

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        log.info(request.getSession().getAttribute("employee") + "退出了登录!");
        request.getSession().removeAttribute("employee");
        return R.success("退出登录成功!");
    }

    @PostMapping
    public R<String> saveEmployee(@RequestBody employee emp, HttpSession session) {
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        try {
            boolean flag = employeeService.save(emp);
            if (!flag) {
                return R.error("添加失败,可能存在用户名冲突");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success("添加员工成功!");
    }

    /**
     * @param page
     * @param pageSize
     * @param name
     * @return com.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     * @description: 分页查询员工
     * @author 86187
     * @date: 2022/11/16 11:59
     */

    @GetMapping("/page")
    public R<Page> selectByCondition(int page, int pageSize, String name) {
        log.info("page: {},pageSize:{},name:{}", page, pageSize, name);//记录日志
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, employee::getName, name);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * @param id
     * @return com.reggie.common.R<com.reggie.entity.employee>
     * @description: 返回修改时, 相应的employee信息
     * @author 86187
     * @date: 2022/11/16 12:26
     */

    @GetMapping("/{id}")
    public R<employee> editEmployeeReturnData(@PathVariable Long id) {
        System.out.println(id);
        LambdaQueryWrapper<employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(employee::getId, id);
        employee emp = employeeService.getOne(queryWrapper);
        return R.success(emp);
    }

    /**
     * @param emp
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 保存状态修改和员工信息的修改
     * @author 86187
     * @date: 2022/11/16 19:56
     */

    @PutMapping()
    public R<String> saveEdit(@RequestBody employee emp, HttpSession session) {
        log.info("{}:进行了修改", (Long) session.getAttribute("employee"));
        boolean update = employeeService.updateById(emp);
        if (update) {
            return R.success("修改成功");
        } else {
            return R.error("更改失败!");
        }
    }
}
